#include "comm.hpp"

struct Init
{
    int fd;
    int shmid;
    char* s;

    Init()
    {
        key_t key = GetKey();
        shmid = CreatShm(key);   //重新定义了，难怪不对，我靠了
        s = (char*)shmat(shmid, nullptr, 0);
        Makefifo();
        fd = open(filename, O_RDONLY);
    }
    ~Init()
    {
        shmdt(s);
        int n = shmctl(shmid, IPC_RMID, nullptr);
        if(n) std::cout << "释放失败" << std::endl;
        close(fd);
        unlink(filename);
    }
};
int main()
{
    // //获得键值
    // key_t key = GetKey();

    // //返回标识符
    // int shmid = CreatShm(key);

    // //分配一个虚拟地址
    // char* s = (char*)shmat(key, nullptr, 0);

    //取消该虚拟地址
    Init init;
    while(true)
    {
        //std::cout << "Client say: " << init.s << std::endl;
        
        int code = 0;
        ssize_t n = read(init.fd, &code, sizeof code);
        if(n > 0)
        {
            std::cout << "Client say: " << init.s << std::endl;
        }
        else if (n == 0)
        {
            break;
        }
    }

    //释放 共享内存
    // shmctl(init.shmid, IPC_RMID, nullptr);
}